package com.glory.dgcpcpda.adapter;

import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.entity.EdcEntity;
import com.glory.dgcpcpda.entity.EdcSetBinEntity;
import com.glory.dgcpcpda.entity.FTLowQualitySamplingEntity;

import java.util.List;

/**
 * FileName: LastBatchAdapter
 * Author: XJH
 * Date: 2020/11/28 0:44
 * Description:
 * History:
 */
public class FTLowQualitySamplingAdapter extends BaseQuickAdapter<FTLowQualitySamplingEntity, BaseViewHolder> {
    public FTLowQualitySamplingAdapter(@Nullable List<FTLowQualitySamplingEntity> data) {
        super(R.layout.adapter_ft_lowqualitysampling, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FTLowQualitySamplingEntity item) {
        helper.setText(R.id.tvItem1,item.getNAME())
                .setText(R.id.etItem1,item.getOSUNHEALTHY())
                .setText(R.id.etItem2,item.getFT1UNHEALTHY())
                .setText(R.id.etItem3,item.getFT2UNHEALTHY());

        EditText etItem1 = helper.getView(R.id.etItem1);
        EditText etItem2 = helper.getView(R.id.etItem2);
        EditText etItem3 = helper.getView(R.id.etItem3);

        setEditListener(etItem1,item,"1");
        setEditListener(etItem2,item,"2");
        setEditListener(etItem3,item,"3");
    }

    private void setEditListener(EditText editText, FTLowQualitySamplingEntity item,String num) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(num.equals(("1"))){
                    if(s.toString().equals("")){
                        item.setOSUNHEALTHY("");
                    }else {
                        item.setOSUNHEALTHY(s.toString());
                    }
                }
                if(num.equals(("2"))){
                    if(s.toString().equals("")){
                        item.setFT1UNHEALTHY("");
                    }else {
                        item.setFT1UNHEALTHY(s.toString());
                    }
                }
                if(num.equals(("3"))){
                    if(s.toString().equals("")){
                        item.setFT2UNHEALTHY("");
                    }else {
                        item.setFT2UNHEALTHY(s.toString());
                    }
                }
            }
        };

        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
